package com.a.lib_utils

import android.app.Activity
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import com.blankj.utilcode.util.ActivityUtils
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog

/**
 * tip依赖于DialogUtil，DialogUtil负责生成，TipUtil负责具体功能
 * */
object TipUtil {
    private var handler = Handler(Looper.getMainLooper())
    private var tipDialog: QMUITipDialog? = null
    private const val DEFAULT_DELAY = 700L

    /*toast*/
    private var toast: Toast? = null

    @JvmStatic
    fun toast(context: Context?, message: String?) {
        if (toast != null) toast!!.cancel()
        toast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
        toast!!.show()
    }

    @JvmStatic
    fun toast(message: String?) {
        if (toast != null) toast!!.cancel()
        toast = Toast.makeText(ActivityUtils.getTopActivity(), message, Toast.LENGTH_SHORT)
        toast!!.show()
    }

    /**
     * tip 如果是activity，自动运行在主线程，不用手动切换
     * */
    @JvmStatic
    fun showTip(
        context: Context,
        @QMUITipDialog.Builder.IconType typ: Int,
        tipWord: String?,
        outside: Boolean,
        cancelable: Boolean
    ) {
        dismiss()
        if (context is Activity) {
            context.runOnUiThread {
                tipDialog = DialogUtil.getTip(context, typ, tipWord, cancelable)
                tipDialog?.setCanceledOnTouchOutside(outside)
                tipDialog?.show()
            }
        } else {
            tipDialog = DialogUtil.getTip(context, typ, tipWord, cancelable)
            tipDialog!!.setCanceledOnTouchOutside(outside)
            tipDialog!!.show()
        }
    }

    @JvmStatic
    @JvmOverloads
    fun successTip(context: Context, tipWord: String?, delay: Long = DEFAULT_DELAY) {
        showTip(context, QMUITipDialog.Builder.ICON_TYPE_SUCCESS, tipWord, outside = true, cancelable = false)
        delayDismiss(delay)
    }

    @JvmStatic
    @JvmOverloads
    fun failTip(context: Context, tipWord: String?, delay: Long = DEFAULT_DELAY) {
        showTip(context, QMUITipDialog.Builder.ICON_TYPE_FAIL, tipWord, outside = true, cancelable = false)
        delayDismiss(delay)
    }

    @JvmStatic
    @JvmOverloads
    fun tip(context: Context, tipWord: String?, delay: Long = DEFAULT_DELAY) {
        showTip(context, QMUITipDialog.Builder.ICON_TYPE_INFO, tipWord, outside = true, cancelable = false)
        delayDismiss(delay)
    }

    @JvmStatic
    fun loadingTip(context: Context, tipWord: String?, delay: Long) {
        showTip(context, QMUITipDialog.Builder.ICON_TYPE_LOADING, tipWord, outside = false, cancelable = true)
        delayDismiss(delay)
    }

    @JvmStatic
    fun loadingTip(context: Context, tipWord: String?) {
        showTip(context, QMUITipDialog.Builder.ICON_TYPE_LOADING, tipWord, outside = false, cancelable = true)
    }

    @JvmStatic
    fun loadingTip(context: Context) {
        showTip(context, QMUITipDialog.Builder.ICON_TYPE_LOADING, "正在加载中...", outside = false, cancelable = true)
    }

    @JvmStatic
    fun dismiss() {
        Log.i("TAG", "dismiss: ")
        if (tipDialog != null) {
            if (tipDialog!!.isShowing)
                tipDialog!!.dismiss()
            tipDialog = null
        }
    }

    @JvmStatic
    fun delayDismiss(delay: Long) {
        handler.postDelayed({ dismiss() }, delay)
    }

    @JvmStatic
    fun delayDismiss() {
        handler.postDelayed({ dismiss() }, DEFAULT_DELAY)
    }
}